package cn.zhiran.org.service.impl;

import cn.zhiran.basic.service.impl.BaseServiceImpl;
import cn.zhiran.org.domain.Employee;
import cn.zhiran.org.domain.Shop;
import cn.zhiran.org.mapper.EmployeeMapper;
import cn.zhiran.org.mapper.ShopMapper;
import cn.zhiran.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service

public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional
    @Override
    public void update(Shop shop) {
        shopMapper.update(shop);
    }

    @Transactional
    @Override
    public void insert(Shop shop) {

        shopMapper.insert(shop);
    }

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        /**
         * 1.先添加employee,再返回自增的主键
         * 2.把employee中返回的id，赋值，再去添加店铺shop，返回自增主键
         * 3.shop_id主键赋值，更新employee，shop_id
         */
        Employee admin = shop.getAdmin();
        //1.判断密码是否一致、
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
                throw new RuntimeException("两次密码不一致");
        }
        //2.判断用户名是否存在
        Employee employee=employeeMapper.findByUser(admin.getUsername());

        if(employee!=null){  //用户名已存在,请直接登录
            throw new RuntimeException("用户名已存在");
        }
        //用户不存在
        //3.先添加employee，再返回自增的主键
        employeeMapper.insert(admin);
        //4.添加店铺，返回自增的主键
        shopMapper.insert(shop);
        //5.把shop_id赋值给admin中的shop_id
        admin.setShop_id(shop.getId());
        //6.更新admin
        employeeMapper.update(admin);

    }
}
